<?php
class PreparationREQImage
{
    //Variables qui contiendront la  pr�paration des requ�tes
    private $ReqSelectById;
    private $ReqSelectByName;
    private $ReqSelectAll;
    private $ReqTrouverID;
    private $ReqTrouverDernierID;
    private $ReqInsert;
    private $ReqTruncate;


    public function __construct($db)
    {
    	//variables renseign�es lors de la pr�paration des requ�tes
	
        $this->ReqSelectAll = $db->prepare("SELECT * FROM image ORDER BY IMTITRE DESC");
		
        $this->ReqSelectById = $db->prepare("SELECT id, name FROM user WHERE id = ?");
       
 	    $this->ReqTrouverDernierID = $db->prepare("SELECT IMID FROM image WHERE IMID = LAST_INSERT_ID()");
        
 	    $this->ReqTrouverID = $db->prepare("SELECT IMID FROM image WHERE IMTITRE = ? AND IMFICHIER = ? IMID_APPARTENIR = ?");
 	    
		$this->ReqInsert = $db->prepare("INSERT INTO image(IM_TITRE, IM_DESCRIPTION, IM_FICHIER, IM_ID_APPARTENIR) VALUES (:IMTITRE, :IMDESCRIPTION, :IMFICHIER, :IMID_APPARTENIR)");
		
       $this->ReqTruncate = $db->prepare("TRUNCATE Image");
	   
    }

//M�thodes publiques qui ex�cuteront les requ�tes d�j� pr�par�es dans le constructeur.
	public function insert($vtitre, $vdescription, $vfichier, $vUnAlbum)
    {
		$this->ReqInsert->execute(array(':IMTITRE' => $vtitre, ':IMDESCRIPTION' => $vdescription, ':IMFICHIER' => $vfichier, ':IMID_APPARTENIR' => $vUnAlbum));
    	
        return $this->ReqTrouverDernierID->fetch();
    }
    
    public function TrouverID ($vtitre, $vfichier, $vUnAlbum)
    {
    	return $this->ReqTrouverID->fetch(array($vtitre, $vfichier, $vUnAlbum));
    }

    public function selectAll()
    {
    	$this->ReqSelectAll->execute();
        return $this->ReqSelectAll->fetchAll();
    }

	public function selectById($id)
    {
        $this->ReqSelectById->execute(array($id));
        return $this->ReqSelectById ->fetch();
    }

    public function selectByName($name)
    {
        $this->ReqSelectByName->execute(array('%'.$name.'%'));
        return $this->ReqSelectByName->fetchAll();
    }
    
	public function truncate()
    {
        return $this->ReqTruncate->execute();
    }
	
}